// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DBDictionaryServiceHelpers.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.Entities
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.Entities;
    using Org.OpenTrader.Framework.Forge.Enums;

    #endregion

    /// <summary>
    /// The db dictionary service helpers.
    /// </summary>
    public partial class DBDictionaryServiceHelpers
    {
        #region Constants and Fields

        /// <summary>
        /// The Identity.
        /// </summary>
        private static readonly Identity Identity = Identity.Create(System.Reflection.MethodBase.GetCurrentMethod());

        #endregion

        #region Public Methods

        /// <summary>
        /// The create dictionary.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <param name="currency">
        /// The currency.
        /// </param>
        /// <returns>
        /// The create dictionary.
        /// </returns>
        public static bool CreateDictionary(Guid dictionaryId, ECurrency currency)
        {
            var dbDictionary = DBDictionaryService.CreateDBDictionaryService(dictionaryId, currency.ToString());
            Singleton<DB>.Instance.Entities.AddToDBDictionaryService(dbDictionary);

            return Singleton<DB>.Instance.Commit() > 0;
        }

        /// <summary>
        /// The exists dictionary.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <returns>
        /// The exists dictionary.
        /// </returns>
        public static bool ExistsDictionary(Guid dictionaryId)
        {
            var dbDictionaries = from d in Singleton<DB>.Instance.Entities.DBDictionaryService where d.ID == dictionaryId select d;

            return dbDictionaries.Count() == 1;
        }

        /// <summary>
        /// The get domestic currency.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <returns>
        /// </returns>
        public static Status<IDictionary<ECurrency, double>> GetCurrencyPrices(Guid dictionaryId)
        {
            var dbDictionaries = from d in Singleton<DB>.Instance.Entities.DBDictionaryService where d.ID == dictionaryId select d;

            if (dbDictionaries.Count() == 1)
            {
                var dbDictionary = dbDictionaries.First();

                IDictionary<ECurrency, double> currencyPrices = new Dictionary<ECurrency, double>();
                dbDictionary.DBCurrencyPrice.Load();
                foreach (var v in dbDictionary.DBCurrencyPrice)
                {
                    currencyPrices[EnumDecoder<ECurrency>.Parse(v.ForeignCurrency)] = v.CurrencyPrice;
                }

                return Status.Success<IDictionary<ECurrency, double>>(Identity, currencyPrices);
            }

            return Status.Failure<IDictionary<ECurrency, double>>(
                Identity, string.Format("Failed to fetch CurrencyPrices for DBDictionaryService with ID {0}", dictionaryId));
        }

        /// <summary>
        /// The get db dictionary service.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <returns>
        /// </returns>
        public static DBDictionaryService GetDBDictionaryService(Guid dictionaryId)
        {
            var dbDictionaries = from d in Singleton<DB>.Instance.Entities.DBDictionaryService where d.ID == dictionaryId select d;

            if (dbDictionaries.Count() == 1)
            {
                return dbDictionaries.First();
            }

            return null;
        }

        /// <summary>
        /// The get domestic currency.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <returns>
        /// </returns>
        public static Status<ECurrency> GetDomesticCurrency(Guid dictionaryId)
        {
            var dbDictionaries = from d in Singleton<DB>.Instance.Entities.DBDictionaryService where d.ID == dictionaryId select d.DomesticCurrency;

            if (dbDictionaries.Count() == 1)
            {
                return Status.Success<ECurrency>(Identity, EnumDecoder<ECurrency>.Parse(dbDictionaries.First()));
            }

            return Status.Failure<ECurrency>(Identity, string.Format("Failed to fetch DomesticCurrency for DBDictionaryService with ID {0}", dictionaryId));
        }

        #endregion
    }
}